package com.epalmpay.usercenter;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.epalmpay.bean.OilMessageBean;
import com.epalmpay.dto.yunliu.UserAuthMessage;
import com.epalmpay.util.HttpClientUtil;
import com.epalmpay.util.RedisClient;
import com.epalmpay.util.Response;
import com.epalmpay.util.payplat.SecurityUtil;
import com.epalmpay.util.SysUtils;

import org.apache.commons.lang.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 云流验证用户信息
 */

public class UserAuthApi {


    public static String md5Key = "YUNLIU2017oeolu3490jg92iu543uvbONAZIWGvwYEEVcdsv56u7g6W3gYerf452";

    /**
     * 到统一用户中心，验证用户信息
     *
     * @return
     */
    public static UserAuthMessage finishAuthUser(String userid, String appMemberSid) {

        String url = "http://app.yunl7.com/thirdpartycall/thirdPartyCall/queryMemberData";
        Map<String, String> params = new HashMap<String, String>();
        params.put("id", userid);
        params.put("app_member_sid", appMemberSid);
        String signSrc = userid + md5Key;
        params.put("sign", SecurityUtil.MD5Hex(signSrc));
        String resultBody = HttpClientUtil.post(url, params);
        JSONObject respond = JSON.parseObject(resultBody);
        UserAuthMessage message = new UserAuthMessage();
        if (respond.getString("status").equalsIgnoreCase("success")) {
            message.setErrcode(1);
			message.setErrmsg("成功");
			JSONObject obj=respond.getJSONObject("obj");
			message.setIdNumber("000000000000000000");
			message.setPhoneNum(obj.getString("phone"));		
			message.setUserId(userid);
			if(StringUtils.isNotBlank(obj.getString("name"))){
				message.setUserName(obj.getString("name"));
			}else{
				JSONObject org=obj.getJSONObject("org");
				message.setUserName(org.getString("name"));
			}
			message.setImgPath(obj.getString("imgPath"));
			
        } else {
            message.setErrcode(0);
            message.setErrmsg(respond.getString("status") + ":" + respond.getString("message"));
        }
        return message;
    }


    /**
     * 到统一用户中心，验证用户信息
     *
     * @return
     */
    public static Response finishPayCall(String orderId, String tradeNo, String totalFee, String notifyUrl) {
        if (StringUtils.isBlank(notifyUrl)) {
            notifyUrl = "http://app.yunl7.com/thirdpartycall/payCall/notify";
        }

        Map<String, String> params = new HashMap<String, String>();
        params.put("orderId", orderId);
        params.put("tradeNo", tradeNo);
        params.put("totalFee", totalFee);
        String signSrc = orderId + tradeNo + totalFee + md5Key;
        params.put("sign", SecurityUtil.MD5Hex(signSrc));
        String resultBody = HttpClientUtil.post(notifyUrl, params);
        JSONObject respond = JSON.parseObject(resultBody);
        Response baseResult = new Response();
        if (respond.getString("status").equalsIgnoreCase("success")) {

            baseResult.setStatus(true);
			baseResult.setMessage("成功");
			return baseResult;
        } else {
            baseResult.setStatus(false);
			baseResult.setMessage(respond.getString("status") + ":" + respond.getString("message"));
			return baseResult;
        }
      
    }


//	--编号：10021，含义：钱包转账通知 ,模板：已经收到{0}转账,金额为{1},余额为{2}  - 600
//	--编号：10022，含义：账户余额变动 ,模板：您的账户余额变动金额{0}，余额{1} - 700
//	--编号：10023，含义：提现 ,模板：提现{0},提现金额为{1}，余额为{2} - 800
//	--编号：10024，含义：发货提醒 ,模板：您购买的商品，订单号为{0}已经发货 - 900
//	--编号：10025，含义：系统消息 ,模板：您收到一条系统消息{0}  - 1000
  //10026       提现 ,模板：信用卡还款{0},还款金额为{1}，余额为{2} - 
 // 10027   您的{0}油卡成功充值金额{1}元      1200
 //  10031  含义： 消费提醒   模板： 您的账号进行了一笔{0},消费金额为{1}--1300

    public static final String MC_SYS_MESSAGE = "10025";

    public static final String MC_WITHDRAW = "10023";

    public static final String MC_TRANSFER = "10021";

    public static final String MC_ORDER_FH = "10024";

    public static final String MC_BLANCE_CHANGE = "10022";
    
    public static final String MC_OILCHARGE_NOTIFY="10027";
	
	public static final String MC_CREDTCARD_WITHDRAW="10026";

	public static final String MC_SALE_PAY = "10031";
    

	
	public static boolean  finishSendMessage(long index,String toUserId,String messageCode,List<String> paramList){
		try {
			OilMessageBean messageBean=new OilMessageBean();
			messageBean.setCode(messageCode); 
			messageBean.setCreateTime(SysUtils.formatDate("yyyy-MM-dd HH:mm:ss"));
			messageBean.setToMemberId(toUserId);
			messageBean.setParamList(paramList);
			messageBean.setType(OilMessageBean.Type.user);
			messageBean.setViewType(OilMessageBean.ViewType.txt);
			RedisClient.getInstance().addMessage(messageBean);
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		return true;
	}
	
	

}
